# -*- coding: utf-8 -*-
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders

class MailNotifier:
    "Clase simple para el envio de emails"
    sender = username = password = ""
    msg = None
    
    def __init__(self,us,pas,smtstring):
        self.username = us
        self.password = pas
        self.sender = smtplib.SMTP(smtstring) #ex 'smtp.gmail.com'
        self.sender.ehlo()
        self.sender.starttls()
        self.sender.login(self.username, self.password)
        
    def sendmail(self,fromaddr,toaddrs,subject,text, intype="text",attachments = []):
        self.msg = MIMEMultipart()
        self.msg.attach(MIMEText(text.encode('utf-8'),intype))
        self.msg['Subject'] = subject
        self.msg['From'] = fromaddr
        self.msg['To'] = toaddrs
        for attachment in attachments:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(open(attachment, 'rb').read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attachment))
            self.msg.attach(part)
        self.sender.sendmail(self.username, toaddrs, self.msg.as_string())
        self.sender.close()